module repository.AnimationLinksRepo;

import d2sqlite3;

import model.Animation;
import model.AnimationVlink;
import utils.ConstValue;

// import std.stdio;

class AnimationLinksRepo
{

    Database db;
    this ()
    {
        db = Database(Const.connStr);
    }

    ~this ()
    {
        db.close();
    }

    /** 
     * 添加动漫视频链接对象(直接添加)
     * Params:
     *   vlink = AnimationVlink
     * Returns: bool
     */
    bool addAnimationLink (AnimationVlink vlink)
    {
        Statement statement = db.prepare(
            "INSERT INTO animation_vlinks (animation_id, name, url, real_url) VALUES (:animation_id, :name, :url, :real_url);");
        statement.bindAll(vlink.animation_id, vlink.name, vlink.url, vlink.real_url);
        statement.execute();
        return db.changes > 0;
    }

    /** 
     * 添加动漫视频链接对象2(不存在则添加)
     * Params:
     *   vlink = AnimationVlink
     * Returns: bool
     */
    bool addAnimationLink2 (AnimationVlink vlink)
    {
        Statement statement = db.prepare("INSERT INTO animation_vlinks(animation_id,name,url) SELECT :said,:sname,:surl WHERE NOT EXISTS (SELECT animation_id,name,url FROM animation_vlinks WHERE animation_id=:animation_id AND name=:name);");
        statement.bind(":said", vlink.animation_id);
        statement.bind(":sname", vlink.name);
        statement.bind(":surl", vlink.url);
        statement.bind(":animation_id", vlink.animation_id);
        statement.bind(":name", vlink.name);
        statement.execute();
        return db.changes > 0;
    }

    /** 
     * 批量添加
     * Params:
     *   vlinkList = AnimationVlink[]
     * Returns: bool
     */
    bool addAnimationLinkBatch (AnimationVlink[] vlinkList)
    {
        import std.format : format;

        auto total = vlinkList.length;
        string sql = "INSERT INTO animation_vlinks (animation_id, name, url) VALUES ";
        string sqlLeft;
        foreach (i, vl; vlinkList)
        {
            sqlLeft = format(`("%s","%s","%s")`, vl.animation_id, vl.name, vl.url);
            sql ~= sqlLeft;
            if (i < total - 1)
            {
                sql ~= ",";
            }
        }
        sql ~= ";";
        db.execute(sql);
        int ch = db.changes;
        if (0 < ch && ch <= total)
        {
            return true;
        }
        return false;
    }

    /** 
     * 获取一个链接
     * Params:
     *   animationVlinkId = uint
     * Returns: AnimationVlink
     */
    AnimationVlink getOneAnimationLink (uint animationVlinkId)
    {
        Statement statement = db.prepare(
            "SELECT id, name, url FROM animation_vlinks WHERE id=:id LIMIT 1");
        statement.bind(":id", animationVlinkId);
        ResultRange results = statement.execute();
        scope (exit)
        {
            statement.reset();
        }

        if (results.empty)
        {
            return null;
        }
        auto row = results.front;
        auto id = row["id"].as!uint;
        auto name = row["name"].as!string;
        auto url = row["url"].as!string;

        return new AnimationVlink(id, name, url);
    }

    /** 
     * 删除链接
     * Params:
     *   animationVlinkId = uint
     * Returns: bool
     */
    bool removeAnimationLink (uint animationVlinkId)
    {
        Statement statement = db.prepare("DELETE FROM animation_vlinks WHERE id=:id");
        statement.bind(":id", animationVlinkId);
        statement.execute();
        return db.changes > 0;
    }

    /** 
     * 编辑链接
     * Params:
     *   animationVlinkId = uint
     *   al = AnimationVlink
     * Returns: bool
     */
    bool editAnimationLink (uint animationVlinkId, AnimationVlink al)
    {
        Statement statement = db.prepare(
            "UPDATE animation_vlinks SET name=:name, url=:url, real_url=:real_url WHERE id=:id");
        statement.bindAll(al.name, al.url, al.real_url, animationVlinkId);
        statement.execute();
        return db.changes > 0;
    }
}
